package net.xumf.support.auth;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class CacheVerification extends VerificationAbstract {

    private Cache<String, List<String>> cache = CacheBuilder.newBuilder()
            .maximumSize(Integer.MAX_VALUE)
            .expireAfterAccess(Integer.MAX_VALUE, TimeUnit.MINUTES)
            .build();


    @Override
    public List<String> getRoles() {
        return cache.getIfPresent(getRoleKey());
    }

    @Override
    public List<String> getPermissions() {
        return cache.getIfPresent(getPermissionKey());
    }

    @Override
    public void setRoles(List<String> roles) {
        cache.put(getRoleKey(), roles);
    }

    @Override
    public void setPermissions(List<String> permissions) {
        cache.put(getPermissionKey(), permissions);
    }

    @Override
    public void clear() {
        cache.invalidate(getRoleKey());
        cache.invalidate(getPermissionKey());
    }
}
